﻿#include "netconfigform.h"
#include "ui_netconfigform.h"
#include"Tool/configrw.h"
#include "Common/configData.h"
#include <QFileDialog>
NetConfigForm::NetConfigForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::NetConfigForm)
{
    ui->setupUi(this);
    ReadCfg();

    ConfigData::strTempImgPath="./temp.png";
}

NetConfigForm::~NetConfigForm()
{
    delete ui;
}

void NetConfigForm::ReadCfg()
{
    ConfigRW* instance= ConfigRW::getInstance("./config.ini");

    ConfigData::strIP = instance->readValue("TRDP","IP").toString();
    ConfigData::strPort = instance->readValue("TRDP","PORT").toString();
    ConfigData::strIP_mvb = instance->readValue("MVB","IP").toString();
    ConfigData::strPort_mvb = instance->readValue("MVB","PORT").toString();
    ConfigData::strIP_EKE = instance->readValue("EKE","IP").toString();
    ConfigData::strPort_EKE = instance->readValue("EKE","PORT").toString();
    ConfigData::strVMsolution_mvb = instance->readValue("MVB","VM_SOL").toString();
    ConfigData::strVMsolution_trdp = instance->readValue("TRDP","VM_SOL").toString();

    ConfigData::snapURL =instance->readValue("OCR","URL_TRDP").toString();
    ConfigData::snapURL_MVB =instance->readValue("OCR","URL_MVB").toString();
    ConfigData::snapWaitTime=instance->readValue("SNAP","WAITTIME").toString();
    ConfigData::imgSavePath=instance->readValue("SNAP","SAVEPATH").toString();
    ConfigData::bSaveOrigion = instance->readValue("SNAP","SAVE_ORIGION").toBool();
    ConfigData::bSaveTargetRegion = instance->readValue("SNAP","SAVE_TARGET_REGION").toBool();

    ui->lineEdit_trdpServerIP->setText( ConfigData::strIP);
    ui->lineEdit_TRDPServerPort->setText(ConfigData::strPort);
    qDebug()<<"Readcfg:"<<ConfigData::strIP<<":"<<ConfigData::strPort;

    //vmSolution
    ui->lineEdit_VMSolution_trdp->setText(ConfigData::strVMsolution_trdp);
    ui->lineEdit_VMSolution_mvb->setText(ConfigData::strVMsolution_mvb);

    ui->lineEdit_IP_mvb->setText( ConfigData::strIP_mvb);
    ui->lineEdit_Port_mvb->setText(ConfigData::strPort_mvb);

    ui->lineEdit_IP_EKE->setText( ConfigData::strIP_EKE);
    ui->lineEdit_Port_EKE->setText(ConfigData::strPort_EKE);
    //URL
    ui->lineEdit_OCRURL->setText( ConfigData::snapURL);
    ui->lineEdit_OCRURL_MVB->setText( ConfigData::snapURL_MVB);
    //snap
    ui->lineEdit_waitSnapTime->setText(ConfigData::snapWaitTime);
    ui->lineEdit_SnapSavePath->setText(ConfigData::imgSavePath);
    ui->checkBox_saveOrigion->setChecked(ConfigData::bSaveOrigion );
    ui->checkBox_saveRectArea->setChecked(ConfigData::bSaveTargetRegion );
    emit CfgChanged();

}

void NetConfigForm::saveCfg()
{
    ConfigRW* instance= ConfigRW::getInstance("./config.ini");
    ConfigData::strIP = ui->lineEdit_trdpServerIP->text();
    ConfigData::strPort =  ui->lineEdit_TRDPServerPort->text();
    ConfigData::strIP_mvb = ui->lineEdit_IP_mvb->text();
    ConfigData::strPort_mvb =  ui->lineEdit_Port_mvb->text();
    ConfigData::strIP_EKE = ui->lineEdit_IP_EKE->text();
    ConfigData::strPort_EKE =  ui->lineEdit_Port_EKE->text();
    ConfigData::snapURL =ui->lineEdit_OCRURL->text();
    ConfigData::snapURL_MVB =ui->lineEdit_OCRURL_MVB->text();
    ConfigData::snapWaitTime = ui->lineEdit_waitSnapTime->text();
    ConfigData::imgSavePath = ui->lineEdit_SnapSavePath->text();
    ConfigData::bSaveOrigion = ui->checkBox_saveOrigion->isChecked();
    ConfigData::bSaveTargetRegion = ui->checkBox_saveRectArea->isChecked();
    ConfigData::strVMsolution_mvb = ui->lineEdit_VMSolution_mvb->text();
    ConfigData::strVMsolution_trdp = ui->lineEdit_VMSolution_trdp->text();


    instance->writeValue("TRDP","IP", ConfigData::strIP);
    instance->writeValue("TRDP","PORT",ConfigData::strPort);

    instance->writeValue("MVB","IP", ConfigData::strIP_mvb);
    instance->writeValue("MVB","PORT",ConfigData::strPort_mvb);

    instance->writeValue("EKE","IP", ConfigData::strIP_EKE);
    instance->writeValue("EKE","PORT",ConfigData::strPort_EKE);
    //URL
    instance->writeValue("OCR","URL_TRDP", ConfigData::snapURL);
    instance->writeValue("OCR","URL_MVB", ConfigData::snapURL_MVB);
    //Snap
    instance->writeValue("SNAP","WAITTIME",ConfigData::snapWaitTime);
    instance->writeValue("SNAP","SAVEPATH",ConfigData::imgSavePath);
    instance->writeValue("SNAP","SAVE_ORIGION",ConfigData::bSaveOrigion);
    instance->writeValue("SNAP","SAVE_TARGET_REGION",ConfigData::bSaveTargetRegion);

    //vmSolution
    instance->writeValue("TRDP","VM_SOL",ConfigData::strVMsolution_trdp);
    instance->writeValue("MVB","VM_SOL",ConfigData::strVMsolution_mvb);
    emit CfgChanged();
    emit SendLogMsg(QString::fromLocal8Bit("配置保存."));
}

QString NetConfigForm::SellectDirPath()
{
    // 父窗口，这里可以传递一个窗口对象，如果没有则传nullptr
    // 对话框标题
    // 默认打开的路径，"." 表示当前目录

    //utf-8 保存的文本不能写中文？
    QString directory = QFileDialog::getExistingDirectory(nullptr,QString::fromLocal8Bit("select directory"),".",QFileDialog::ShowDirsOnly|QFileDialog::DontResolveSymlinks);

    return directory;
}

void NetConfigForm::on_pushButton_apply_clicked()
{
    saveCfg();
}



void NetConfigForm::on_pushButton_selectDir_clicked()
{
    QString selDir= SellectDirPath();
    ui->lineEdit_SnapSavePath->setText(selDir);
}


QString NetConfigForm::getVmSolutionPath()
{
    QString strTemp = QStringLiteral("Choose solution path");
    QString strFileName = QFileDialog::getOpenFileName(this, strTemp, "", "SOL(*.sol)");
    if (!strFileName.isEmpty())
    {
        return strFileName;
    }

    return QString();
}



void NetConfigForm::on_btn_selVMsol_trdp_clicked()
{
    QString strFileName=  getVmSolutionPath();
    ui->lineEdit_VMSolution_trdp->setText(strFileName);
}


void NetConfigForm::on_btn_selVMsol_mvb_clicked()
{
    QString strFileName=  getVmSolutionPath();
    ui->lineEdit_VMSolution_mvb->setText(strFileName);
}

